home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 23 / CU Amiga - Super CD-ROM 23 (June 1998).iso / CUCD / Magazine / C_Tutorial / Part-7 / arexx0 / idcmp.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-11-26  |  5.6 KB  |  229 lines

  1. #include "idcmp.h"
  2. #include "drawwin.h"
  3. #include "gadgets.h"
  4. #include "loadsave.h"
  5. #include "menu.h"
  6. #include "toolwin.h"
  7. #include "arexx.h"
  8.  
  9. #include<string.h>
  10.  
  11. #include<clib/exec_protos.h>
  12. #include<clib/gadtools_protos.h>
  13. #include<clib/graphics_protos.h>
  14. #include<clib/intuition_protos.h>
  15.  
  16. static void doGadgetUp(struct Window*, UWORD, struct Gadget*);
  17. static int  doMenuPick(struct Window*, UWORD);
  18. static int  doARexx(struct RexxMsg*);
  19.  
  20. /* Our message handling code */
  21. void handleIDCMP()
  22. {
  23.     char* text = "Hello World!";
  24.     int going = TRUE;
  25.     int drawing = FALSE;
  26.     ULONG drawsig, toolsig, arexxsig, gotsig;
  27.     struct Window* drawwin = getDrawWin();
  28.     drawsig = 1 << drawwin->UserPort->mp_SigBit;
  29.     arexxsig = getARexxSig();
  30.     while(going)
  31.     {
  32.         struct IntuiMessage* intuimsg;
  33.         /* Only include tool window signal mask if window is open */
  34.         toolsig = getToolSig();
  35.         /* Wait for messages to arrive */
  36.         gotsig = Wait(drawsig | toolsig | arexxsig);
  37.         /* Messages have arrived: loop through all of them */
  38.         /* Check messages from the drawing window first */
  39.         if(gotsig & drawsig)
  40.         {
  41.             while(intuimsg = GT_GetIMsg(drawwin->UserPort))
  42.             {
  43.                 /* Copy the important bits of the message */
  44.                 ULONG class = intuimsg->Class;
  45.                 UWORD code = intuimsg->Code;
  46.                 WORD mousex = intuimsg->MouseX;
  47.                 WORD mousey = intuimsg->MouseY;
  48.                 /* Reply when finished copying bits from message */
  49.                 GT_ReplyIMsg(intuimsg);
  50.                 /* Act on this message... */
  51.                 switch(class)
  52.                 {
  53.                 case IDCMP_MOUSEBUTTONS:
  54.                     switch(code)
  55.                     {
  56.                     case SELECTDOWN:
  57.                         drawing = TRUE;
  58.                         break;
  59.                     case SELECTUP:
  60.                         drawing = FALSE;
  61.                         break;
  62.                     }
  63.                     /* break; omitted so we draw on click, too */
  64.                 case IDCMP_MOUSEMOVE:
  65.                     if(drawing)
  66.                     {
  67.                         Move(drawwin->RPort, mousex, mousey);
  68.                         Text(drawwin->RPort, text, strlen(text));
  69.                     }
  70.                     break;
  71.                 case IDCMP_MENUPICK:
  72.                     going = doMenuPick(drawwin, code);
  73.                     drawwin = getDrawWin();
  74.                     drawsig = 1 << drawwin->UserPort->mp_SigBit;
  75.                     break;
  76.                 }
  77.             }
  78.         }
  79.         /* Now check messages from the tool window */
  80.         if(going && (gotsig & toolsig))
  81.         {
  82.             struct Window* toolwin = getToolWin();
  83.             while(toolwin && (intuimsg = GT_GetIMsg(toolwin->UserPort)))
  84.             {
  85.                 /* Copy the important bits of the message */
  86.                 ULONG class = intuimsg->Class;
  87.                 UWORD code = intuimsg->Code;
  88.                 APTR iaddr = intuimsg->IAddress;
  89.                 /* Reply when finished copying bits from message */
  90.                 GT_ReplyIMsg(intuimsg);
  91.                 /* Act on this message... */
  92.                 switch(class)
  93.                 {
  94.                 case IDCMP_CLOSEWINDOW:
  95.                     closeToolWin();
  96.                     /* Update our local toolwin, so we stop loop */
  97.                     toolwin = NULL;
  98.                     uncheckToolBar(drawwin);
  99.                     break;
  100.                 case IDCMP_REFRESHWINDOW:
  101.                     /* You *MUST* remember to ask for and handle these refresh messages */
  102.                     GT_BeginRefresh(toolwin);
  103.                     GT_EndRefresh(toolwin, TRUE);
  104.                     break;
  105.                 case IDCMP_GADGETUP:
  106.                     doGadgetUp(drawwin, code, (struct Gadget*)iaddr);
  107.                     break;
  108.                 case IDCMP_MENUPICK:
  109.                     going = doMenuPick(drawwin, code);
  110.                     /* Update our local toolwin, so we stop loop */
  111.                     toolwin = getToolWin();
  112.                     drawwin = getDrawWin();
  113.                     drawsig = 1 << drawwin->UserPort->mp_SigBit;
  114.                     break;
  115.                 }
  116.             }
  117.         }
  118.         /* Now check messages from the ARexx port */
  119.         if(going && (gotsig & arexxsig))
  120.         {
  121.             struct RexxMsg* msg;
  122.             while(going && (msg = getARexxMsg()))
  123.                 going = doARexx(msg);
  124.         }
  125.     }
  126. }
  127.  
  128. /* Process IDCMP_GADGETUP event */
  129. static void doGadgetUp(struct Window* drawwin, UWORD code, struct Gadget* gad)
  130. {
  131.     switch(gad->GadgetID)
  132.     {
  133.     case MYBUT_ID:
  134.         /* Our button was clicked!  Set foreground to next pen colour */
  135.         nextFgPen(drawwin);
  136.         break;
  137.     case MYPAL_ID:
  138.         /* Our palette gadget was clicked!  Set foreground to gadget colour */
  139.         setFgPen(drawwin, code);
  140.         break;
  141.     }
  142. }
  143.  
  144. /* Process IDCMP_MENUPICK event */
  145. static int doMenuPick(struct Window* drawwin, UWORD code)
  146. {
  147.     UWORD menuCode, menuNumber, itemNumber;
  148.     /* Loop over all the menu selections in the menu code */
  149.     struct MenuItem* item;
  150.     for(menuCode = code;
  151.             menuCode != MENUNULL;
  152.             menuCode = item->NextSelect)
  153.     {
  154.         item = ItemAddress(drawwin->MenuStrip, menuCode);
  155.         /* Extract the menu number and menu item number from the menu code */
  156.         menuNumber = MENUNUM(menuCode);
  157.         itemNumber = ITEMNUM(menuCode);
  158.         /* Now decide what to do based on what menu item was selected */
  159.         switch(menuNumber)
  160.         {
  161.         case 0:  /* Project menu */
  162.             /* Only one item: Quit */
  163.             switch(itemNumber)
  164.             {
  165.             case 0:  /* Load */
  166.                 return load();
  167.             case 1:  /* Save */
  168.                 save();
  169.                 break;
  170.             case 3:  /* Quit (item 2 is the bar!) */
  171.                 return FALSE;
  172.             }
  173.             break;
  174.         case 1:  /* Pen menu */
  175.             switch(itemNumber)
  176.             {
  177.             case 0:  /* Next */
  178.                 nextFgPen(drawwin);
  179.                 break;
  180.             case 1:  /* Prev */
  181.                 prevFgPen(drawwin);
  182.                 break;
  183.             case 3:  /* Reset (item 2 is the bar!) */
  184.                 resetFgPen(drawwin);
  185.                 break;
  186.             }
  187.             break;
  188.         case 2:  /* Tools menu */
  189.             switch(itemNumber)
  190.             {
  191.             case 0:  /* Screen Bar */
  192.                 ShowTitle(drawwin->WScreen, item->Flags & CHECKED);
  193.                 break;
  194.             case 1:  /* Tool Bar */
  195.                 /* Do the open or close */
  196.                 if(item->Flags & CHECKED)
  197.                 {
  198.                     /* If the open fails, stop immediately */
  199.                     if(!openToolWin())
  200.                         return FALSE;
  201.                 }
  202.                 else
  203.                     closeToolWin();
  204.                 break;
  205.             }
  206.         }
  207.     }
  208.     /* Keep going */
  209.     return TRUE;
  210. }
  211.  
  212. /* Process an ARexx message */
  213. static int doARexx(struct RexxMsg* msg)
  214. {
  215.     int going = TRUE;
  216.     /* By default, our reply will indicate an error */
  217.     LONG rc = 20;
  218.     char* result = NULL;
  219.     if(strcmp(msg->rm_Args[0], "QUIT") == 0)
  220.     {
  221.         going = FALSE;
  222.         /* We recognised the command, so set rc to zero */
  223.         rc = 0;
  224.         result = "Hello Painter is quitting";
  225.     }
  226.     replyARexxMsg(msg, rc, result);
  227.     return going;
  228. }
  229.